package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.FindBanner;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.custom.FindTopicCard;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.util.DotOper;
import reduce.fz.com.reduce.util.DotUtil;
import reduce.fz.com.reduce.util.PicassoUtil;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 * Created by qingshengzheng on 15/7/5.
 */
public class FindAdapter extends HeaderFooterStatusRecyclerViewAdapter<FindAdapter.ContentFindView> {
    private List<TeamCategory> findfolders = new ArrayList<TeamCategory>();
    private Context context;
    private LayoutInflater mLayoutInflater;
    private FindListener findListener;


    public FindAdapter(Context context, List<TeamCategory> findfolders, FindListener findListener) {
        if (findfolders != null) {
            this.findfolders = findfolders;
        }
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.findListener = findListener;
    }

    private void clearData() {
        findfolders.clear();
        notifyDataSetChanged();
    }

    public void addFolderList(List<TeamCategory> folders) {
        int start = this.findfolders.size();
        int limit = folders.size();
        this.findfolders.addAll(folders);
        notifyItemRangeInserted(start, limit);
    }

    @Override
    public ContentFindView createFooterStatusViewHolder(View footerView) {
        return new ContentFindView(footerView, this, findListener) {
            public void renderView(Object findContent, int position) {

            }
        };
    }

    @Override
    protected int getHeaderItemCount() {
        return 0;
    }

    @Override
    protected int getContentItemCount() {
        return findfolders.size();
    }

    @Override
    protected ContentFindView onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        return null;
    }

    @Override
    protected ContentFindView onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View v = null;
        ContentFindView holder = null;
        v = mLayoutInflater.inflate(R.layout.custom_find_view_card, parent, false);
        holder = new ContentFindView(v, this, findListener);
        return holder;
    }

    @Override
    protected int getContentItemViewType(int position) {
        return 0;
    }

    @Override
    protected void onBindHeaderItemViewHolder(ContentFindView headerViewHolder, int position) {
    }

    @Override
    protected void onBindContentItemViewHolder(ContentFindView contentViewHolder, int position) {
        contentViewHolder.renderView(this.findfolders.get(position), position);

    }

    public static interface FindListener {
        public void onClickFolder(TeamCategory folder);
    }

    public static class ContentFindView extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView find_category_backimage;
        private ImageView find_category_icon;
        private TextView find_category_name;
        private RelativeLayout rl_view_card;
        private Resources mResources;
        private View view;
        private FindListener findListener;
        private TeamCategory teamCategory;


        public ContentFindView(View itemView, FindAdapter adapter, FindListener findListener) {
            super(itemView);
            this.view = itemView;
            this.findListener=findListener;
            mResources=itemView.getResources();
            rl_view_card = (RelativeLayout) itemView.findViewById(R.id.rl_view_card);
            find_category_backimage = (ImageView) itemView.findViewById(R.id.find_category_backimage);
            find_category_icon = (ImageView) itemView.findViewById(R.id.find_category_icon);
            find_category_name = (TextView) itemView.findViewById(R.id.find_category_name);
        }

        public void renderView(TeamCategory teamCategory, int postion) {
            resetPadding(view, postion);
            this.teamCategory =teamCategory;
           // find_category_name.setText(teamCategory.getCategoryName());
            //PicassoUtil.laodUrl(itemView.getContext(), teamCategory.getCategoryUrl(), find_category_backimage);
            //find_category_backimage.setOnClickListener(this);
            //PicassoUtil.laodUrl(itemView.getContext(), teamCategory.getIconUrl(), find_category_icon);
        }


        public void resetPadding(View cardView, int position) {
            Boolean isFirstLine = false;
            Boolean isFirstCol = false;
            Boolean isLastCol = false;
            int paddingTop = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding);
            int paddingBottom = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding_bottom);
            int paddingLeft = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding);
            int paddingRight = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding);
            //第一行无上边距
            if (position <= 2)
                isFirstLine = true;
            if (position % 3 == 0)
                isFirstCol = true;
            if (position % 3 == 2)
                isLastCol = true;
            if (isFirstLine) paddingTop = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding_small);
            if (isFirstCol) paddingLeft = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding_small);;
            if (isLastCol) paddingRight = mResources.getDimensionPixelSize(R.dimen.grid_layout_padding_small);;
            int a=ScreenUtils.getScreenW();
            int width=(ScreenUtils.getScreenW()-2*paddingLeft-2*paddingRight)/3;
            int height=width;
            ViewGroup.LayoutParams lp=cardView.getLayoutParams();
            lp.width=width;
            lp.height=height;
            cardView.setLayoutParams(lp);
            cardView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

            //第一列无做边距
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.find_category_backimage:
                    findListener.onClickFolder(teamCategory);
                    break;
                default:
                    break;

            }
        }
    }


}
